package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.mapper.CourseMapper;
import com.demo.mapper.TeacherMapper;
import com.demo.pojo.entity.Course;
import com.demo.pojo.entity.Teacher;
import com.demo.pojo.dto.CourseListDTO;
import com.demo.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程服务实现类
 */
@Service
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Override
    public IPage<CourseListDTO> getCourseList(Page<Course> page, Long courseNo, String courseName, 
                                             String courseType, String dept, Long teacherId) {
        // 构建查询条件
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        
        if (courseNo != null) {
            queryWrapper.eq(Course::getCourseNo, courseNo);
        }
        if (StringUtils.hasText(courseName)) {
            queryWrapper.like(Course::getCourseName, courseName);
        }
        if (StringUtils.hasText(courseType)) {
            queryWrapper.eq(Course::getCourseType, courseType);
        }
        if (StringUtils.hasText(dept)) {
            queryWrapper.like(Course::getDept, dept);
        }
        if (teacherId != null) {
            queryWrapper.eq(Course::getTeacherId, teacherId);
        }
        
        queryWrapper.orderByDesc(Course::getUpdateDate);
        
        // 执行分页查询
        IPage<Course> coursePage = courseMapper.selectPage(page, queryWrapper);
        
        // 转换为DTO
        List<CourseListDTO> dtoList = coursePage.getRecords().stream().map(course -> {
            CourseListDTO dto = new CourseListDTO();
            BeanUtils.copyProperties(course, dto);
            
            // 查询教师信息
            if (course.getTeacherId() != null) {
                Teacher teacher = teacherMapper.selectById(course.getTeacherId());
                if (teacher != null) {
                    dto.setTeacherName(teacher.getTeName());
                }
            }
            
            return dto;
        }).collect(Collectors.toList());
        
        // 构建返回结果
        Page<CourseListDTO> resultPage = new Page<>(coursePage.getCurrent(), coursePage.getSize(), coursePage.getTotal());
        resultPage.setRecords(dtoList);
        
        return resultPage;
    }
    
    @Override
    public Course getCourseById(Long id) {
        return courseMapper.selectById(id);
    }
    
    @Override
    public Course getCourseByNo(Long courseNo) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getCourseNo, courseNo);
        return courseMapper.selectOne(queryWrapper);
    }
    
    @Override
    public boolean addCourse(Course course) {
        // 检查课程编号是否已存在
        Course existingCourse = getCourseByNo(course.getCourseNo());
        if (existingCourse != null) {
            throw new RuntimeException("课程编号已存在");
        }
        
        // 设置更新时间
        course.setUpdateDate(new java.util.Date());
        
        return courseMapper.insert(course) > 0;
    }
    
    @Override
    public boolean updateCourse(Course course) {
        // 检查课程是否存在
        Course existingCourse = getCourseById(course.getCourseId());
        if (existingCourse == null) {
            throw new RuntimeException("课程不存在");
        }
        
        // 如果课程编号发生变化，检查新编号是否已存在
        if (!existingCourse.getCourseNo().equals(course.getCourseNo())) {
            Course courseWithSameNo = getCourseByNo(course.getCourseNo());
            if (courseWithSameNo != null && !courseWithSameNo.getCourseId().equals(course.getCourseId())) {
                throw new RuntimeException("课程编号已存在");
            }
        }
        
        // 设置更新时间
        course.setUpdateDate(new java.util.Date());
        
        return courseMapper.updateById(course) > 0;
    }
    
    @Override
    public boolean deleteCourse(Long id) {
        return courseMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean deleteCourses(List<Long> ids) {
        return courseMapper.deleteBatchIds(ids) > 0;
    }
    
    @Override
    public List<Course> getAllCourses() {
        return courseMapper.selectList(null);
    }
}

























